<?php

namespace Plugins\AdminBasic\Models;

use App\Models\Attributes\InitData;
use App\Models\Model;
use Carbon\Carbon;
use App\Models\Attributes\CreateSql;
use Plugins\AdminBasic\Enums\BasicAdministratorsEnumStatus;


/**
 * 管理员
 * 
 * @property int    $id
 * @property string $name         用户名
 * @property string $account      账号
 * @property string $password     密码
 * @property int    $error_number 密码错误次数
 * @property Carbon $lately_time  最近登录
 * @property Carbon $error_date   错误日期
 * @property BasicAdministratorsEnumStatus $status       状态
 * @property array  $role_ids     角色
 * @property int    $create_id    创建人
 * @property Carbon $create_time  创建时间
 * @property Carbon $update_time  修改时间
 * @property int    $delete_time  删除时间
 */
#[CreateSql(<<<SQL
CREATE TABLE `sd_administrators` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(32) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户名',
  `account` varchar(32) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '账号',
  `password` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '密码',
  `error_number` tinyint(2) NOT NULL DEFAULT '0' COMMENT '密码错误次数',
  `lately_time` datetime DEFAULT NULL COMMENT '最近登录',
  `error_date` date DEFAULT NULL COMMENT '错误日期',
  `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态：1=正常，2=冻结',
  `role_ids` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '角色',
  `create_id` int(11) NOT NULL DEFAULT '0' COMMENT '创建人',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `update_time` datetime NOT NULL COMMENT '修改时间',
  `delete_time` int(11) DEFAULT NULL COMMENT '删除时间',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `account` (`account`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='管理员'
SQL)]
#[InitData([
    ['id' => 1, 'name' => '超管', 'account' => 'admin', 'password' => '$2y$10$q.Pv4rJMlb9kznF4zZn6KO6GxkSt5ojOCjUo8xjzBXrT2Tahrbyly', 'error_number' => 0, 'lately_time' => '2020-01-01 00:00:00', 'status' => 1, 'role_ids' => '[]', 'create_id' => 1, 'create_time' => '2020-01-01 00:00:00', 'update_time' => '2020-01-01 00:00:00']
])]
class BasicAdministrators extends Model
{


    protected array $casts = [
        ...self::DEFAULT_CASTS,
        'id' => 'int',
        'error_number' => 'int',
        'lately_time' => 'datetime:Y-m-d H:i:s',
        'error_date' => 'date:Y-m-d',
        'role_ids' => 'array',
        'create_id' => 'int'
    ];

    protected array $fillable = ['name', 'account', 'password', 'error_number', 'lately_time', 'error_date', 'status', 'role_ids', 'create_id'];

    protected ?string $table = 'basic_administrators';

    public function getStatusAttribute($value): BasicAdministratorsEnumStatus
    {
        return BasicAdministratorsEnumStatus::from($value);
    }

    public function setStatusAttribute($value): void
    {
        $this->attributes['status'] = $value instanceof BasicAdministratorsEnumStatus ? $value->value : $value;
    }
}